//
//  shopClassView.m
//  ILiangCang
//
//  Created by ZX on 16-4-16.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "shopClassView.h"
#import "httpEngine.h"
#import "classCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation shopClassView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addTableView];

    }
        return self;
}

-(void)addTableView
{
    
    tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-136)];
    tabView.delegate =self;
    tabView.dataSource = self;
    [self addSubview:tabView];
    [tabView registerClass:[classCell class] forCellReuseIdentifier:@"classCell"];
    tabView.rowHeight = 50;

    NSArray * homeArray = @[@"儿童用品",@"家具",@"家居配饰",@"厨卫用品"];
    NSArray * dressArray = @[@"男士",@"女士",@"儿童"];
    NSArray * otherArray = @[@"工具",@"玩具",@"出版",@"数码",@"户外用品",@"美容护肤"];
    NSArray * artArray = @[@"绘画",@"摄影",@"雕塑"];
    NSArray * giftArray = @[@"送他",@"送她",@"送儿童"];
    _dataArray = @[homeArray,dressArray,otherArray,artArray,giftArray];
    tabView.rowHeight = 40;
    [tabView reloadData];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_flag[section] == YES) {
        
        NSArray * array  =_dataArray[section];
        
        return array.count;
        
    }else
    {
        // 如果返回0,这个区一行也没有
        return 0;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * indentifer = [NSString stringWithFormat:@"classCell"];
    classCell * cell = [tableView dequeueReusableCellWithIdentifier:indentifer forIndexPath:indexPath];

    NSArray * array = _dataArray[indexPath.section];
    cell.title.text = array[indexPath.row];

    
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return WIDTH/7*3.5;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH/7*3.5)];
    UIButton * button  =[UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = view.frame;
    button.tag = section +100;
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
   
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"header%ld.jpg",section+1]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openOrClose:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];

    
    return view;
    
}
- (void)openOrClose:(UIButton *)button
{

    // 把c数组中判断区是否展开的值进行取反
    _flag[button.tag - 100] = !_flag[button.tag -100];

    NSIndexSet *indexSet  =[NSIndexSet indexSetWithIndex:button.tag -100];
    
    [tabView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

@end
